package com.gnerv.pabu.basic.core.service.impl;

import com.gnerv.pabu.basic.core.service.IKafkaListenerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class KafkaListenerServiceImpl implements IKafkaListenerService {

    @Autowired
    private KafkaListenerEndpointRegistry registry;

    @Override
    public boolean startListener(String listenerId) {
        try {
            //判断监听容器是否启动，未启动则将其启动
            if (!registry.getListenerContainer(listenerId).isRunning()) {
                registry.getListenerContainer(listenerId).start();
            }
            //项目启动的时候监听容器是未启动状态，而resume是恢复的意思不是启动的意思
            registry.getListenerContainer(listenerId).resume();
        } catch (Exception e) {
            log.info("kafka消费端监听器" + listenerId + "启动失败", e);
            return false;
        }
        return true;
    }

    @Override
    public boolean stopListener(String listenerId) {
        try {
            registry.getListenerContainer(listenerId).stop();
        } catch (Exception e) {
            log.info("kafka消费端监听器" + listenerId + "停止失败", e);
            return false;
        }
        return true;
    }
}
